package org.mg.engine.impl;

import com.google.common.base.Strings;
import org.mg.engine.InstructAbstract;
import org.mg.engine.core.Session;
import org.msg.commons.InstructUtils;
import org.msg.commons.RespStatus;

import java.io.IOException;
import java.net.Socket;
import java.util.Objects;

/**
 * Create by bo.chen in 2018/10/13 0:05
 */
public class SendInstruct extends InstructAbstract {
    @Override
    public void execute(String data)  {
        if(!validata(data)) {
            return;
        }
        try {
            if(data.indexOf(" ") <= 0) {
                error();
                return;
            }
            String key = InstructUtils.getInstruct(data);
            if(Strings.isNullOrEmpty(key)) {
                error();
                return;
            }
            String message = InstructUtils.getParams(data);
            if(Strings.isNullOrEmpty(message)) {
                error();
                return;
            }
            Session session = CACHE.get(key);
            if(Objects.isNull(session)) {
                error();
                return;
            }
            session.println(key,message);
            success();
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
        error();
    }
}
